/**
 * @依赖
 * jqueryui 1.11.0 core
 * artTemplate http://aui.github.com/artTemplate/
 * 
 * @sample
 * $('#page-bar').pagebar({
 *	pageNo: 1,
 *	pageSize: 10,
 *	rowCount: 0,
 *	callback: function(pageNo, pageSize) {},
 *	itemTotalNumber: 10
 * });
 * 
 * var options = $('#page-bar').pagebar('option');
 *  
 * try {
 *	$('#page-bar').pagebar('destroy');
 * } catch(e) {
 *	// ignore
 * }
 */
 
$.widget( "ui.pagebar", {
	version: "1.11.0",
//	defaultElement: "<div>",
	options: {
		pageNo: 1,
		pageSize: 10,
		rowCount: 0,
		// callback params: pageNo, pageSize
		callback: undefined,
		itemTotalNumber: 10
	},
	_status: {
		hasContainerClass: false
	},
	_callback: function(event) {
		var $this = $(this);
		if(event.data.callback && !$this.hasClass('active')) {
			var pageNo = $this.attr('data-page-no');
			var callback = event.data.callback;
			callback(pageNo, event.data.pageSize);
		}
	},
	_create: function() {
		var pageNo = this.options.pageNo,
		pageSize = this.options.pageSize,
		rowCount = this.options.rowCount,
		itemTotalNumber = this.options.itemTotalNumber,
		
		beginOffsetSize = (0 == itemTotalNumber % 2) ? itemTotalNumber/2 - 1 : Math.floor(itemTotalNumber/2),
		pageCount = 0 == Math.ceil(rowCount/pageSize) ? 1 : Math.ceil(rowCount/pageSize),
		begin = pageNo - beginOffsetSize >= 1 ? pageNo - beginOffsetSize : 1,
		itemsHtml = "";
		var end = begin + itemTotalNumber - 1;
		end = end <= pageCount ? end : pageCount;
		
		// 没有下一页的时候, 尽可能展示最多的 pagebarItem
		if(end == pageCount) {
			var offsetSize2 = itemTotalNumber - (end - pageNo);
			begin = pageNo - offsetSize2 >= 1 ? pageNo - offsetSize2 : 1;
		}
		
		if(pageCount == 1) {
			return "";
		}
		
		var pageItemHtml = '<a class="page-bar-item" data-page-no="{{pageNo}}"><span>{{pageLabel}}</span></a>';
		var activePageItemHtml = '<a class="page-bar-item active" data-page-no="{{pageNo}}"><span>{{pageLabel}}</span></a>';
		var prevPageItemHtml = '<a class="page-bar-item previous" data-page-no="{{pageNo}}"><span>{{pageLabel}}</span></a>';
		var nextPageItemHtml = '<a class="page-bar-item next" data-page-no="{{pageNo}}"><span>{{pageLabel}}</span></a>';
		
		var renderPageItem = template.compile(pageItemHtml);
		var renderActivePageItem = template.compile(activePageItemHtml);
		var renderPrevPageItem = template.compile(prevPageItemHtml);
		var renderNextPageItem = template.compile(nextPageItemHtml);
		
		for(var i = pageNo; i >= begin; i--) {
			if(i == pageNo) {
//				itemsHtml = $(renderPageItem({pageNo: i})).addClass('active').html() + itemsHtml;
				itemsHtml = renderActivePageItem({pageNo: i, pageLabel: i}) + itemsHtml;
			} else {
				itemsHtml = renderPageItem({pageNo: i, pageLabel: i}) + itemsHtml;
			}
		}
		if(begin > 1) {
			itemsHtml = renderPrevPageItem({pageNo: pageNo - 1, pageLabel: '上一页'}) + itemsHtml;
		}
		
		for(var i = pageNo + 1; i <= end; i++) {
			var resultItemHtml = renderPageItem({pageNo: i, pageLabel: i});
			itemsHtml = itemsHtml + resultItemHtml;
		}
		if(end < pageCount) {
			itemsHtml = itemsHtml + renderNextPageItem({pageNo: pageNo + 1, pageLabel: '下一页'});
		}
		
		if(this.element.hasClass('page-bar')) {
			this._status.hasContainerClass = true;
		} else {
			this.element.addClass('page-bar');
		}
		
		this.element.on('click', '.page-bar-item', this.options, this._callback).append(itemsHtml);
	},

	_init: function() {
		
	},
	
	_destroy: function() {
		if(!this._status.hasContainerClass) {
			this.element.removeClass('page-bar');
		}
		
		this.element.unbind('click', this._callback).empty();
	}
});